'''
Created on 30/03/2009

@author: eh01
'''
from xml.etree import ElementTree
from yarra import helper
from yarra.datastore import dsconstants
from yarra.datastore.dso import DSO
from yarra.datastore.dst import DST
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
from yarra.handler import messagetypeconstants

class ConnectivityTestHTTPRequestHandler(BaseEventHandler):
    '''
    Handles the POST for Connection Test Request
    '''
    
    def __init__(self):
        '''
        Constructor
        '''
        
    def do(self, data):
        dso = data
        http_request = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_HTTP_REQUEST).get_literal()
        lock = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_LOCK).get_literal()
                    
        test_string_dst = dso.get_dst(subject=dsconstants.SUBJECT_FORM_DATA,
                    predicate=dsconstants.PREDICATE_TEST_STRING)
        if test_string_dst:
            test_string = test_string_dst.get_literal()
        else:
            test_string = ""
        dst1 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_TYPE, 
                  literal=messagetypeconstants.CONNECTIVITY_TEST_REQUEST_MESSAGE_TYPE)
        dst2 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_DESTINATION_NODE, 
                  literal="node_spear")
        dst3 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_CONTENT, 
                  object=dsconstants.SUBJECT_MESSAGE_CONTENT)
        dst4 = DST(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                  predicate=dsconstants.PREDICATE_TEST_STRING, 
                  literal=test_string)
        dst5 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_HTTP_REQUEST, 
                  literal=http_request)       
        dst6 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_LOCK, 
                  literal=lock)
        new_dso = DSO([dst1, dst2, dst3, dst4, dst5, dst6])
        event = Event(eventconstants.SEND_TO_HUB_EVENT_TYPE, new_dso)
        EventManager().add_event(event)  
        
class ConnectivityTestWebServiceRequestHandler(BaseEventHandler):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def do(self, data):
        dso = data
        
        # create web service name dst
        web_service_name_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_SPEAR_A2A_WEB_SERVICE_NAME,
            literal='LxSpearEcho')
        
        # create soap message
        access_token = helper.get_access_token()
        env = ElementTree.Element('{http://schemas.xmlsoap.org/soap/envelope/}Envelope')
        body = ElementTree.SubElement(env, '{http://schemas.xmlsoap.org/soap/envelope/}Body')
        echo = ElementTree.SubElement(body, '{http://test.webservice.spear.lx.dse.vic.gov.au}echo')
        echo.append(access_token)
        input = ElementTree.SubElement(echo,
            '{http://test.webservice.spear.lx.dse.vic.gov.au}input')
        test_string_dst = dso.get_dst(subject=dsconstants.SUBJECT_MESSAGE_CONTENT,
                                 predicate=dsconstants.PREDICATE_TEST_STRING)
        input.text = test_string_dst.get_literal()
        soap = '<?xml version="1.0" encoding="utf-8" ?>\r\n' + ElementTree.tostring(env)
        
        soap_message_dst = DST(subject=dsconstants.SUBJECT_ROOT,
            predicate=dsconstants.PREDICATE_SOAP_MESSAGE,
            literal=soap)        
        
        web_service_response_event_type_dst = DST(subject=dsconstants.SUBJECT_ROOT, 
            predicate=dsconstants.PREDICATE_WEB_SERVICE_RESPONSE_EVENT_TYPE,
            literal=eventconstants.CONNECTIVITY_TEST_WEB_SERVICE_RESPONSE_EVENT_TYPE)
        
        # update dso
        dso.add_dst(soap_message_dst)
        dso.add_dst(web_service_name_dst)
        dso.add_dst(web_service_response_event_type_dst)
        dso.remove_dst(dst=test_string_dst)
        dso.remove_dst(subject=dsconstants.SUBJECT_ROOT,
                       predicate=dsconstants.PREDICATE_MESSAGE_CONTENT)
        
        event = Event(type=eventconstants.SEND_TO_SPEAR_EVENT_TYPE, data=dso)
        EventManager().add_event(event)